package com.lq.util.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 * @author 吃人的肉
 * QQ:376870344<br>
 * email:liuqingrou@163.com
 */
public class LqGoInsertAndUpdateAndSelect {
	
	
    private String[] colnames; // 列名数组
    private String[] colTypes; //列名类型数组
    private int[] colSizes; //列名大小数组
	
	//数据库连接
    private static String URL ="";
    private static String NAME = "";
    private static String PASS = "";
    private static String DRIVER ="";
	
	
    /**
     * @param path 包路径 com.lq.entity
     * @param tableName 表名
     */
    public LqGoInsertAndUpdateAndSelect(String tableName,String url,String driver){
    	URL=url.split("\\*\\*\\*")[0];
   		NAME=url.split("\\*\\*\\*")[1];
   		PASS=url.split("\\*\\*\\*")[2];
		DRIVER=driver;
		m(tableName);
    }
	
	/**
	 * 
	 * @param tableName 表名
	 */
	public LqGoInsertAndUpdateAndSelect(String tableName){
		LqConfig conf= new LqConfig();
		conf.init("lqjdbc.properties");
		URL=conf.getString("dataSource.allurl").split("\\*\\*\\*")[0];
	   	NAME=conf.getString("dataSource.allurl").split("\\*\\*\\*")[1];
	   	PASS=conf.getString("dataSource.allurl").split("\\*\\*\\*")[2];
	   	DRIVER=conf.getString("dataSource.driverClassName");
		m(tableName);
	}
	
	private void m(String tableName){
		//创建连接
        Connection con = null;
        //查要生成实体类的表
        String sql = "select * from " + tableName;
        PreparedStatement pStemt = null;
        try {
            try {
                Class.forName(DRIVER);
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            con = DriverManager.getConnection(URL,NAME,PASS);
            pStemt = con.prepareStatement(sql);
            ResultSetMetaData rsmd = pStemt.getMetaData();
            int size = rsmd.getColumnCount();   //统计列
            colnames = new String[size];
            colTypes = new String[size];
            colSizes = new int[size];
            for (int i = 0; i < size; i++) {
                colnames[i] = rsmd.getColumnName(i + 1);
                colTypes[i] = rsmd.getColumnTypeName(i + 1);
                colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
            }
             
            String insert = "insert into "+tableName;
            String update="update "+tableName+" set ";
            
            insert+=" (";
            for (int i = 0; i < colnames.length; i++) {
            	if (colnames.length==i+1) {
            		update+=colnames[i].toLowerCase()+"=?";
            		insert+=colnames[i].toLowerCase();
				}else{
					update+=colnames[i].toLowerCase()+"=?,";
					insert+=colnames[i].toLowerCase()+",";
				}
			}
            insert+=") ";
            insert+="values";
            insert+=" (";
            for (int i = 0; i < colnames.length; i++) {
            	if (colnames.length==i+1) {
            		insert+="?";
				}else{
					insert+="?,";
				}
			}
            insert+=") ";
            update+=" where id=? ";
            System.out.println(insert);
            System.out.println(update);
             
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
          try {
              con.close();
          } catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
        }
	}

}
